<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm">
        <el-form-item :label="$t('msgCenter.bulletin.sendDate')">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('msgCenter.bulletin.state')">
          <el-select v-model="searchForm.noticeStatus" clearable :placeholder="$t('msgCenter.bulletin.prompt.state')">
            <el-option
              v-for="item in $t('msgCenter.bulletin.status')"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('msgCenter.bulletin.theme')">
          <el-input v-model="searchForm.subject" clearable :placeholder="$t('msgCenter.bulletin.prompt.theme')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="primary" @click="addItem">
        {{ $t('msgCenter.bulletin.add') }}
      </el-button>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%;margin-top: 10px"
      >
        <el-table-column
          fixed="left"
          prop="id"
          :label="tableText['id']"
          min-width="80"
        />
        <el-table-column
          fixed="left"
          prop="subject"
          :label="tableText['theme']"
          min-width="100"
        />
        <el-table-column
          prop="pushTime"
          :label="tableText['showTime']"
          min-width="300"
        >
          <template slot-scope="scope">
            {{ scope.row['pushTime'] && scope.row['pushTimeEnd'] ? scope.row['pushTime'] + '-' + scope.row['pushTimeEnd'] : '' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          :label="tableText['createTime']"
          min-width="170"
        />
        <el-table-column
          :label="tableText['updater']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ scope.row['modifier'] ? scope.row['modifier'] : scope.row['operator'] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="modifyTime"
          :label="tableText['updateTime']"
          min-width="170"
        />
        <el-table-column
          prop="noticeStatus"
          :label="tableText['state']"
          min-width="100"
        >
          <template slot-scope="scope">
            {{ scope.row.noticeStatus !== null ? setValue(scope.row.noticeStatus, 'status') : '' }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          :label="$t('common.operate.name')"
          min-width="200"
        >
          <template slot-scope="scope">
            <el-button v-if="scope.row['noticeStatus'] === 1 || scope.row['noticeStatus'] === 2" size="small" @click="showItem(scope.row)">
              {{ $t('msgCenter.bulletin.show') }}
            </el-button>
            <el-button v-if="scope.row['noticeStatus'] === 0" size="small" @click="editItem(scope.row)">
              {{ $t('msgCenter.bulletin.edit') }}
            </el-button>
            <el-button v-if="scope.row['noticeStatus'] === 0" size="small" @click="deleteItem(scope.row)">
              {{ $t('common.operate.delete') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <bulletin-dialog
      ref="bulletinDialog"
      :show-dialog="editDialog"
      :title="title"
      :state="state"
      :choose-item="chooseItem"
      @change-dialog="changeDialog"
      @edit-success="editSuccess"
    />
    <delete-dialog
      ref="delete-dialog"
      :delete-state="deleteState"
      :loading="deleteLoading"
      @change-delete="changeDelete"
      @delete-this="deleteThis"
    />
  </el-row>
</template>

<script>
import { getNotice, deleteMsgPush } from '@/api/operation'
import { formatDate } from '@/utils/method'
import BulletinDialog from '@/components/operation/bulletin-dialog'
import DeleteDialog from '@/components/delete/index.vue'
export default {
  name: 'Bulletin',
  components: {
    BulletinDialog,
    DeleteDialog
  },
  data() {
    return {
      loading: false,
      editDialog: false,
      deleteState: false,
      deleteLoading: false,
      tableData: [],
      tableText: {},
      totalNumber: 0,
      title: null,
      time: null,
      searchForm: this.params(),
      state: null,
      chooseItem: {
        id: null,
        subject: null,
        pushTime: null,
        pushTimeEnd: null,
        pushType: 2,
        pushContent: null,
        pushObj: null,
        isNotice: true
      }
    }
  },
  mounted() {
    this.tableText = this.$t('msgCenter.bulletin')
    this.getData()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        pushTimeStart: null,
        pushTimeEnd: null,
        subject: '',
        pushStatus: null,
        noticeStatus: '',
        isNotice: true
      }
    },
    setValue(value, data) {
      const map = this.$t(`msgCenter.bulletin.${data}`)
      const name = map.filter(item => {
        return item.value.toString() === value.toString()
      })
      return (name.length > 0 && name[0].desc) ? name[0].desc : ''
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.time = null
      this.getData()
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['pushTimeStart'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['pushTimeEnd'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['pushTimeStart'] = null
        data['pushTimeEnd'] = null
      }
      return data
    },
    getData() {
      this.loading = true
      getNotice(this.searchData())
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    changeDialog(state) {
      this.editDialog = state
    },
    addItem() {
      this.title = this.$t('msgCenter.bulletin.add')
      this.state = 'add'
      this.choose({})
      this.editDialog = true
    },
    editSuccess() {
      this.choose({})
      this.getData()
    },
    showItem(item) {
      this.title = this.$t('msgCenter.bulletin.show')
      this.state = 'view'
      this.choose(item)
      this.editDialog = true
    },
    editItem(item) {
      this.title = this.$t('msgCenter.bulletin.edit')
      this.state = 'edit'
      this.choose(item)
      this.editDialog = true
    },
    choose(param) {
      this.chooseItem.id = param.id
      this.chooseItem.subject = param.subject
      this.chooseItem.pushTime = param.pushTime
      this.chooseItem.pushTimeEnd = param.pushTimeEnd
      this.chooseItem.pushContent = param.pushContent
      this.chooseItem.pushObj = param.pushObj
      if (param.pushTime && param.pushTimeEnd) {
        this.$refs.bulletinDialog.setTime(param.pushTime, param.pushTimeEnd)
      }
    },
    changeDelete(param) {
      this.deleteState = param
    },
    deleteItem(data) {
      this.chooseItem.id = data.id
      this.changeDelete(true)
    },
    deleteThis() {
      this.deleteLoading = true
      const id = this.chooseItem.id
      deleteMsgPush(id)
        .then(res => {
          this.deleteLoading = false
          if (res.success) {
            this.$message.success(this.$t('common.tip.deleteSuccess'))
            this.deleteState = false
            this.getData()
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.deleteLoading = false
          this.$message.error(err.message)
        })
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card{
    margin: 10px;
  }
</style>
